﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OnlineStore.Business_Logic_Layer;
using OnlineStore.Data_Access_Layer;


namespace OnlineStore
{
    public partial class frmImport : Form
    {
        BizPurchase eBizPurchase;
        BizImport eBizImport;
        public frmImport()
        {
            InitializeComponent();
            eBizPurchase = new BizPurchase();
            eBizImport = new BizImport();
        }

        private void frmImport_Load(object sender, EventArgs e)
        {
            LoadPurchaseList("Status", "New");
        }
        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                // Get purchase header by id
                tbPurchase ePurchase = eBizPurchase.GetPurchasebyKeyWord("ID", txtPurchaseID.Text).First();
                // fill data into form.
                List<tbPurchaseDetail> PurchaseDetailList = eBizPurchase.GetPurchaseDetailbyPurchaseID(int.Parse(txtPurchaseID.Text));
                // fill data into datagrid dgOrdertail
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // Check stock on store        
            if (eBizImport.ValidateData(dgPurchaseDetail.Rows))
            {                
                // Update status of Order                
                eBizPurchase.UpdatePurchaseStatus("Received", int.Parse(txtPurchaseID.Text));
                // Save Export detail
                eBizImport.SaveImport(int.Parse(txtPurchaseID.Text), dgPurchaseDetail.Rows, dCreatedDate.Text.ToString());
                
                tabControl1.SelectedTab = tabPur;
                LoadPurchaseList("Status", "New");
                ResetValue();
            }
        }

        private void LoadPurchaseList(string strKey, string strValue)
        {
            List<tbPurchase> PurchaseList = eBizPurchase.GetPurchasebyKeyWord(strKey, strValue);
            tbPurchase ePurchase = new tbPurchase();
            dgPurchaseList.Rows.Clear();
            for (int i = 0; i < PurchaseList.Count; i++)
            {
                ePurchase = PurchaseList.ElementAt<tbPurchase>(i);

                string[] NewRow = new string[] { ePurchase.Purchase_ID.ToString(), ePurchase.Supplier_Name.ToString(), ePurchase.Supplier_Address.ToString(), ePurchase.Created_Date.ToString(), ePurchase.Status.ToString() };
                dgPurchaseList.Rows.Add(NewRow);
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabImport;
            try
            {
                txtPurchaseID.Text = dgPurchaseList.Rows[dgPurchaseList.SelectedCells[0].RowIndex].Cells[0].Value.ToString().Trim();
                LoadPurchase();
            }
            catch (Exception ex)
            {
                ex.ToString();

            }
        }
        private void LoadPurchase()
        {
            dgPurchaseDetail.Rows.Clear();
            BizProduct eBizProduct = new BizProduct();
            // Get order header by id
            tbPurchase ePurchase = eBizPurchase.GetPurchasebyKeyWord("ID", txtPurchaseID.Text).First();            
            // fill data into form.
            txtSupplierName.Text = ePurchase.Supplier_Name;
            txtSuppAddress.Text = ePurchase.Supplier_Address;
            txtShipTo.Text = ePurchase.Ship_To;
            txtStatus.Text = ePurchase.Status;
            // Get Purchase detail
            List<tbPurchaseDetail> ePurchaseDetailList = eBizPurchase.GetPurchaseDetailbyPurchaseID(int.Parse(txtPurchaseID.Text));
            // fill data into datagrid dgOrdertail
            for (int i = 0; i < ePurchaseDetailList.Count; i++)
            {
                tbProduct eProduct = eBizProduct.GetProductbyKeyWord("ID", ePurchaseDetailList[i].Product_ID.ToString()).First();
                string[] NewRow = new string[] { ePurchaseDetailList[i].Qty.ToString(), ePurchaseDetailList[i].Qty.ToString(), ePurchaseDetailList[i].Product_ID.ToString(), eProduct.Product_Name.ToString(), eProduct.Color.ToString(), eProduct.Size.ToString(), ePurchaseDetailList[i].Price.ToString() };
                dgPurchaseDetail.Rows.Add(NewRow);
            }
        }

        private void txtKeyWord_TextChanged(object sender, EventArgs e)
        {
            LoadPurchaseList(cbKey.Text, txtKeyWord.Text);
        }
        private void ResetValue()
        {
            txtPurchaseID.Text = "";
            txtShipTo.Text = "";
            txtSuppAddress.Text = "";
            txtSupplierName.Text = "";
            txtStatus.Text = "";
            dgPurchaseDetail.Rows.Clear();
        }

        private void txtPurchaseID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {         
                LoadPurchase();
            }
        }        
    }
}
